package ReaderAndWriter;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class TestFileReader {
    public static void main(String[] args) {
        try {
            //test1();
            test2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test1() throws IOException {
        Reader reader=null;
        try {
            reader=new FileReader("F:\\123.txt");
            int i;
            StringBuilder builder=new StringBuilder();
            while ((i= reader.read()) !=-1){
                char character = (char) i;
                builder.append(character);
            }
            System.out.println(builder.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader !=null) reader.close();
        }
    }

    private static void test2() throws IOException {
        Reader reader=null;
        try {
            reader=new FileReader("F:\\123.txt");
            char[] chars=new char[1024];
            StringBuilder builder=new StringBuilder();
            int lengh;

            while ((lengh= reader.read(chars)) !=-1){
                builder.append(chars,0,lengh);
            }
            System.out.println(builder.toString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader !=null) reader.close();
        }
    }

}
